package com.youkeyi.ddy.cloud.basic.crm.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.order.entity.SysVaccineCustomerOrder;
import com.youkeyi.ddy.cloud.basic.crm.order.service.SysVaccineCustomerOrderService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.CrmCustomerVaccineOrderCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.dto.CrmCustomerVaccineOrderListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.dto.CrmCustomerVaccineOrderListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.order.param.CrmCustomerVaccineOrderUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicCrmConstant;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 疫苗客户订单信息 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-04-03
 */
@Service
public class SysVaccineCustomerOrderController implements CrmCustomerVaccineOrderCloudService {

    @Autowired
    private SysVaccineCustomerOrderService sysVaccineCustomerOrderService;

    @Override
    public CrmCustomerVaccineOrderListCloudDto query(CrmCustomerVaccineOrderQueryCloudParam param) {
        CrmCustomerVaccineOrderListCloudDto dto = new CrmCustomerVaccineOrderListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getCustomerVaccineOrderPrimaryKeys())) {
            param.getCustomerVaccineOrderPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getFinanceCorpKeys())) {
            param.getFinanceCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCustomerKeys())) {
            param.getCustomerKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getRecipientKeys())) {
            param.getRecipientKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getGoodsCategoryKeys())) {
            param.getGoodsCategoryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getStatuses())) {
            param.getStatuses().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysVaccineCustomerOrder> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getCustomerVaccineOrderPrimaryKeys()), SysVaccineCustomerOrder::getPrimaryKey, param.getCustomerVaccineOrderPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysVaccineCustomerOrder::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getFinanceCorpKeys()), SysVaccineCustomerOrder::getFinanceCorpKey, param.getFinanceCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCustomerKeys()), SysVaccineCustomerOrder::getCustomerKey, param.getCustomerKeys())
                .in(CollectionUtils.isNotEmpty(param.getRecipientKeys()), SysVaccineCustomerOrder::getRecipientKey, param.getRecipientKeys())
                .in(CollectionUtils.isNotEmpty(param.getGoodsCategoryKeys()), SysVaccineCustomerOrder::getGoodsCategoryKey, param.getGoodsCategoryKeys())
                .in(CollectionUtils.isNotEmpty(param.getStatuses()), SysVaccineCustomerOrder::getStatus, param.getStatuses())
                .eq(StringUtils.isNotBlank(param.getSourceDepartmentKey()), SysVaccineCustomerOrder::getSourceDepartmentKey, param.getSourceDepartmentKey())
                .eq(StringUtils.isNotBlank(param.getSourceUserKey()), SysVaccineCustomerOrder::getSourceUserKey, param.getSourceUserKey())
                .eq(StringUtils.isNotBlank(param.getServiceUserKey()), SysVaccineCustomerOrder::getServiceUserKey, param.getServiceUserKey())
                .like(StringUtils.isNotBlank(param.getCustomerNameFuzzy()), SysVaccineCustomerOrder::getCustomerName, param.getCustomerNameFuzzy())
                .like(StringUtils.isNotBlank(param.getGoodsNameFuzzy()), SysVaccineCustomerOrder::getGoodsName, param.getGoodsNameFuzzy())
                .between(null != param.getRegistrationTimeBegin() && null != param.getRegistrationTimeEnd(), SysVaccineCustomerOrder::getRegistrationTime, param.getRegistrationTimeBegin(), param.getRegistrationTimeEnd())
                .between(null != param.getCompletionTimeBegin() && null != param.getCompletionTimeEnd(), SysVaccineCustomerOrder::getCompletionTime, param.getCompletionTimeBegin(), param.getCompletionTimeEnd())
                .ne(SysVaccineCustomerOrder::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysVaccineCustomerOrder> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysVaccineCustomerOrder> customerOrderList = sysVaccineCustomerOrderService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(customerOrderList.getRecords())) {
            dto.setTotal(customerOrderList.getTotal());

            List<CrmCustomerVaccineOrderListItemCloudDto> items = new ArrayList<>();
            customerOrderList.getRecords().forEach(customerOrder -> {
                CrmCustomerVaccineOrderListItemCloudDto item = new CrmCustomerVaccineOrderListItemCloudDto();
                item.setCustomerVaccineOrderPrimaryKey(customerOrder.getPrimaryKey());
                item.setCorpKey(customerOrder.getCorpKey());
                item.setFinanceCorpKey(customerOrder.getFinanceCorpKey());
                item.setCustomerKey(customerOrder.getCustomerKey());
                item.setCustomerName(customerOrder.getCustomerName());
                item.setGoodsKey(customerOrder.getGoodsKey());
                item.setGoodsName(customerOrder.getGoodsName());
                item.setGoodsCategoryKey(customerOrder.getGoodsCategoryKey());
                item.setGoodsCategoryName(customerOrder.getGoodsCategoryName());
                item.setServiceUserKey(customerOrder.getServiceUserKey());
                item.setServiceUserName(customerOrder.getServiceUserName());
                item.setRecipientKey(customerOrder.getRecipientKey());
                item.setRecipientName(customerOrder.getRecipientName());
                item.setRecipientSex(customerOrder.getRecipientSex());
                item.setRecipientBirthday(customerOrder.getRecipientBirthday());
                item.setRecipientIdCard(customerOrder.getRecipientIdCard());
                item.setIncomeRmb(customerOrder.getIncomeRmb());
                item.setRemark(customerOrder.getRemark());
                item.setSourceDepartmentKey(customerOrder.getSourceDepartmentKey());
                item.setSourceDepartmentName(customerOrder.getSourceDepartmentName());
                item.setSourceUserName(customerOrder.getSourceUserName());
                item.setSourceUserKey(customerOrder.getSourceUserKey());
                item.setRegistrationTime(customerOrder.getRegistrationTime() != null ? customerOrder.getRegistrationTime().getTime() : null);
                item.setCompletionTime(customerOrder.getCompletionTime() != null ? customerOrder.getCompletionTime().getTime() : null);
                item.setStatus(customerOrder.getStatus());
                item.setStatusName(YoukeyiDdyCloudBasicCrmConstant.CRM_ORDER_STATUS.getName(customerOrder.getStatus()));
                item.setConsultantWechat(customerOrder.getConsultantWechat());
                item.setVaccineDoses(customerOrder.getVaccineDoses());
                item.setGovernmentFundingFees(customerOrder.getGovernmentFundingFees());
                item.setCostPrice(customerOrder.getCostPrice());
                item.setIncomeHkb(customerOrder.getIncomeHkb());
                item.setHandlingFeeAmount(customerOrder.getHandlingFeeAmount());
                item.setPayTypeKey(customerOrder.getPayTypeKey());
                item.setPayTypeName(customerOrder.getPayTypeName());
                item.setPrePaymentDate(customerOrder.getPrePaymentDate() != null ? customerOrder.getPrePaymentDate().getTime() : null);
                item.setLaboratoryKey(customerOrder.getLaboratoryKey());
                item.setLaboratoryName(customerOrder.getLaboratoryName());
                item.setDoctorKey(customerOrder.getDoctorKey());
                item.setDoctorName(customerOrder.getDoctorName());
                item.setSourceChannel(customerOrder.getSourceChannel());
                item.setIsDeleted(customerOrder.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(customerOrder.getCreateTime() != null ? customerOrder.getCreateTime().getTime() : null);
                item.setCreateUserKey(customerOrder.getCreateUserKey());
                item.setCreateUserName(customerOrder.getCreateUserName());
                item.setUpdateTime(customerOrder.getUpdateTime() != null ? customerOrder.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(customerOrder.getUpdateUserKey());
                item.setUpdateUserName(customerOrder.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmCustomerVaccineOrderCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getCustomerKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysVaccineCustomerOrder customerOrder = new SysVaccineCustomerOrder();
        customerOrder.setPrimaryKey(SimpleUUidUtils.getUuid());
        customerOrder.setCorpKey(param.getCorpKey());
        customerOrder.setFinanceCorpKey(param.getFinanceCorpKey());
        customerOrder.setCustomerKey(param.getCustomerKey());
        customerOrder.setCustomerName(param.getCustomerName());
        customerOrder.setGoodsKey(param.getGoodsKey());
        customerOrder.setGoodsName(param.getGoodsName());
        customerOrder.setGoodsCategoryKey(param.getGoodsCategoryKey());
        customerOrder.setGoodsCategoryName(param.getGoodsCategoryName());
        customerOrder.setServiceUserKey(param.getServiceUserKey());
        customerOrder.setServiceUserName(param.getServiceUserName());
        customerOrder.setRecipientKey(param.getRecipientKey());
        customerOrder.setRecipientName(param.getRecipientName());
        customerOrder.setRecipientSex(param.getRecipientSex());
        customerOrder.setRecipientBirthday(param.getRecipientBirthday());
        customerOrder.setRecipientIdCard(param.getRecipientIdCard());
        customerOrder.setIncomeRmb(param.getIncomeRmb());
        customerOrder.setRemark(param.getRemark());
        customerOrder.setSourceDepartmentKey(param.getSourceDepartmentKey());
        customerOrder.setSourceDepartmentName(param.getSourceDepartmentName());
        customerOrder.setSourceUserName(param.getSourceUserName());
        customerOrder.setSourceUserKey(param.getSourceUserKey());
        customerOrder.setCompletionTime(param.getCompletionTime() != null ? new Date(param.getCompletionTime()) : null);
        customerOrder.setRegistrationTime(param.getRegistrationTime() != null ? new Date(param.getRegistrationTime()) : null);
        customerOrder.setStatus(param.getStatus());
        customerOrder.setConsultantWechat(param.getConsultantWechat());
        customerOrder.setVaccineDoses(param.getVaccineDoses());
        customerOrder.setGovernmentFundingFees(param.getGovernmentFundingFees());
        customerOrder.setCostPrice(param.getCostPrice());
        customerOrder.setIncomeHkb(param.getIncomeHkb());
        customerOrder.setHandlingFeeAmount(param.getHandlingFeeAmount());
        customerOrder.setPayTypeKey(param.getPayTypeKey());
        customerOrder.setPayTypeName(param.getPayTypeName());
        customerOrder.setPrePaymentDate(param.getPrePaymentDate() != null ? new Date(param.getPrePaymentDate()) : null);
        customerOrder.setLaboratoryKey(param.getLaboratoryKey());
        customerOrder.setLaboratoryName(param.getLaboratoryName());
        customerOrder.setDoctorKey(param.getDoctorKey());
        customerOrder.setDoctorName(param.getDoctorName());
        customerOrder.setSourceChannel(param.getSourceChannel());        customerOrder.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        customerOrder.setCreateUserKey(param.getCreateUserKey());
        customerOrder.setCreateUserName(param.getCreateUserName());
        customerOrder.setCreateTime(new Date());
        boolean save = sysVaccineCustomerOrderService.save(customerOrder);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(customerOrder.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmCustomerVaccineOrderUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getCustomerVaccineOrderPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<SysVaccineCustomerOrder> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SysVaccineCustomerOrder::getPrimaryKey, param.getCustomerVaccineOrderPrimaryKey());
        SysVaccineCustomerOrder customerOrder = new SysVaccineCustomerOrder();
        customerOrder.setPrimaryKey(param.getCustomerVaccineOrderPrimaryKey());
        customerOrder.setFinanceCorpKey(param.getFinanceCorpKey());
        customerOrder.setCustomerKey(param.getCustomerKey());
        customerOrder.setCustomerName(param.getCustomerName());
        customerOrder.setGoodsKey(param.getGoodsKey());
        customerOrder.setGoodsName(param.getGoodsName());
        customerOrder.setGoodsCategoryKey(param.getGoodsCategoryKey());
        customerOrder.setGoodsCategoryName(param.getGoodsCategoryName());
        customerOrder.setServiceUserKey(param.getServiceUserKey());
        customerOrder.setServiceUserName(param.getServiceUserName());
        customerOrder.setRecipientKey(param.getRecipientKey());
        customerOrder.setRecipientName(param.getRecipientName());
        customerOrder.setRecipientSex(param.getRecipientSex());
        customerOrder.setRecipientBirthday(param.getRecipientBirthday());
        customerOrder.setRecipientIdCard(param.getRecipientIdCard());
        customerOrder.setIncomeRmb(param.getIncomeRmb());
        customerOrder.setRemark(param.getRemark());
        customerOrder.setSourceDepartmentKey(param.getSourceDepartmentKey());
        customerOrder.setSourceDepartmentName(param.getSourceDepartmentName());
        customerOrder.setSourceUserName(param.getSourceUserName());
        customerOrder.setSourceUserKey(param.getSourceUserKey());
        customerOrder.setCompletionTime(param.getCompletionTime() != null ? new Date(param.getCompletionTime()) : null);
        customerOrder.setRegistrationTime(param.getRegistrationTime() != null ? new Date(param.getRegistrationTime()) : null);
        customerOrder.setStatus(param.getStatus());
        customerOrder.setConsultantWechat(param.getConsultantWechat());
        customerOrder.setVaccineDoses(param.getVaccineDoses());
        customerOrder.setGovernmentFundingFees(param.getGovernmentFundingFees());
        customerOrder.setCostPrice(param.getCostPrice());
        customerOrder.setIncomeHkb(param.getIncomeHkb());
        customerOrder.setHandlingFeeAmount(param.getHandlingFeeAmount());
        customerOrder.setPayTypeKey(param.getPayTypeKey());
        customerOrder.setPayTypeName(param.getPayTypeName());
        customerOrder.setPrePaymentDate(param.getPrePaymentDate() != null ? new Date(param.getPrePaymentDate()) : null);
        customerOrder.setLaboratoryKey(param.getLaboratoryKey());
        customerOrder.setLaboratoryName(param.getLaboratoryName());
        customerOrder.setDoctorKey(param.getDoctorKey());
        customerOrder.setDoctorName(param.getDoctorName());
        customerOrder.setSourceChannel(param.getSourceChannel());
        customerOrder.setUpdateUserKey(param.getUpdateUserKey());
        customerOrder.setUpdateUserName(param.getUpdateUserName());
        customerOrder.setUpdateTime(new Date());
        boolean save = sysVaccineCustomerOrderService.update(customerOrder, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmCustomerVaccineOrderDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getCustomerVaccineOrderPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysVaccineCustomerOrder> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysVaccineCustomerOrder::getPrimaryKey, param.getCustomerVaccineOrderPrimaryKeys());

        SysVaccineCustomerOrder customerOrder = new SysVaccineCustomerOrder();
        customerOrder.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        customerOrder.setUpdateUserKey(param.getUpdateUserKey());
        customerOrder.setUpdateUserName(param.getUpdateUserName());
        customerOrder.setUpdateTime(new Date());
        boolean save = sysVaccineCustomerOrderService.update(customerOrder, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
